package com.bawei.module_home.ui.merchant

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.AllEventBus
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityMerchantDetailBinding
import com.bawei.module_home.http.HomeServiceViewModel
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

//import org.greenrobot.eventbus.EventBus

@AndroidEntryPoint
@Route(path = Router.MERCHANT_DETAIL_PAGE)
class MerchantDetailActivity : BaseActivity<HomeServiceViewModel,  ActivityMerchantDetailBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_merchant_detail
    private val mViewModel: HomeServiceViewModel by viewModels()
    @Autowired(name = "merchantID")
    @JvmField
    var merchantId=0


    override fun initView() {
        super.initView()
        mBinding.toobar.apply {
            back.setOnClickListener({
                finish()
            })
            title.text="入驻申请详情"
            more.visibility= View.GONE
        }
    }

    override fun initData() {
        mViewModel.getMerchantDetail(merchantId)
        mBinding.b1.setOnClickListener({
            if(mViewModel.merchantDetailItem.value?.status=="已审核"){
                return@setOnClickListener
            }
            mViewModel.approveMerchant(merchantId)
        })


    }

    override fun initListener() {
        mViewModel.merchantDetailState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    mBinding.applicationNumber.text=mViewModel.merchantDetailItem.value?.applicationNumber
                    mBinding.entryTime1.text=mViewModel.merchantDetailItem.value?.entryTime
                    mBinding.entryTime2.text=mViewModel.merchantDetailItem.value?.entryTime
                    mBinding.entryTime3.text=mViewModel.merchantDetailItem.value?.entryTime
                    mBinding.enterpriseName.text=mViewModel.merchantDetailItem.value?.enterpriseName
                    mBinding.contactName.text=mViewModel.merchantDetailItem.value?.contactName
                    mBinding.contactMethod.text=mViewModel.merchantDetailItem.value?.contactMethod
                    mBinding.applicationArea.text=mViewModel.merchantDetailItem.value?.applicationArea.toString()
                    mBinding.entryPersonnel.text=mViewModel.merchantDetailItem.value?.entryPersonnel.toString()
                    mBinding.states.text=mViewModel.merchantDetailItem.value?.status
                    mBinding.b1.text=mViewModel.merchantDetailItem.value?.status
                    if(mViewModel.merchantDetailItem.value?.status.equals("新建")){
                        mBinding.processDot2.background=ContextCompat.getDrawable(mBinding.root.context,
                            R.drawable.vip_rounded_corners2
                        )
                    }else{
                        mBinding.processDot2.background=ContextCompat.getDrawable(mBinding.root.context,
                            R.drawable.area_status_green
                        )
                    }

                }
            }
        }

        mViewModel._mApproveMerchantState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    EventBus.getDefault().post(AllEventBus.RefreshEvent(true, "user_data"))
//                    EventBus.getDefault().post(MerchantApprovedEvent(merchantId))
                    ToastUtils.showLong(this,"审核成功")
                    finish()
                }
            }
        }
    }
    // 审核状态变更事件
    data class MerchantApprovedEvent(val merchantId: Int)


}